/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_CONNECT_IMPL_H
#define SLE_CONNECT_IMPL_H

#include <condition_variable>
#include <mutex>
#include <chrono>
#include "sle_utils_observer_list.h"
#include "i_sle_connect.h"
#include "sle_connect_interface.h"
#include "sle_connect_callback_stub.h"
#include "sle_connect_parcel.h"
#include "sle_address_parcel.h"
#include "sle_host_interface.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
class SleConnectManager::SleConnectManagerImpl {
public:
    SleConnectManagerImpl();
    ~SleConnectManagerImpl();
    SleErrorCode CheckOperation();

    class SleConnectCallbackStubImpl;
    sptr<SleConnectCallbackStubImpl> callbackImpl_ = nullptr;

    sptr<ISleConnect> proxy_ = nullptr;
    sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
    SleObserverList<SleConnectManagerCallback> callbacks_;

    std::condition_variable conditionVariable_;
    bool ready_ = false;
    std::mutex mtx_;
    std::mutex mutexProxy_;
    int8_t rssi_ = 0;
    SleErrorCode errCode_ = SLE_ERR_UNKNOWN;

private:
    void GetProxy();
    void ResetService(const wptr<IRemoteObject> &remote);
};

class SleConnectManager::SleConnectManagerImpl::SleConnectCallbackStubImpl : public SleConnectCallbackStub {
public:
    SleConnectCallbackStubImpl(SleConnectManager::SleConnectManagerImpl &impl) : impl_(impl) {}
    ~SleConnectCallbackStubImpl() = default;

    void OnConnectStateChange(uint16_t connectId, const SleAddress &address, int32_t state, int32_t pairState, uint16_t reason) override
    {
        SleDeviceAddress addr = address.ConvertToSleDeviceAddress();
        impl_.callbacks_.ForEach([connectId, &addr, state, pairState, reason](std::shared_ptr<SleConnectManagerCallback> callback) {
            callback->OnConnectStateChange(connectId, addr, (SleConnectState)state, (SlePairState)pairState, SleDisConnectReason(reason));
        });
    }

    void OnConnectParamUpdateComplete(uint16_t connectId, uint16_t interval, uint16_t latency, uint16_t timeout, uint32_t errCode) override
    {
        SleConnectionParamUpdateEvt connParam = {
            .interval = interval,
            .latency = latency,
            .supervisionTimeout = timeout
        };
        impl_.callbacks_.ForEach([connectId, &connParam, errCode](std::shared_ptr<SleConnectManagerCallback> callback) {
            callback->OnConnectParamUpdateComplete(connectId, connParam, (SleErrorCode)errCode);
        });
    }

    void OnAuthComplete(uint16_t connectId, const SleAddress &address, const SleAuthenticationInfo &info, uint32_t errCode) override
    {
        SleDeviceAddress addr = address.ConvertToSleDeviceAddress();
        SleAuthInfo authInfo;
        info.ConvertToSleAuthInfo(authInfo);
        impl_.callbacks_.ForEach([connectId, &addr, &authInfo, errCode](std::shared_ptr<SleConnectManagerCallback> callback) {
            callback->OnAuthComplete(connectId, addr, authInfo, SleErrorCode(errCode));
        });
    }

    void OnPairComplete(uint16_t connectId, const SleAddress &address, uint32_t errCode) override
    {
        SleDeviceAddress addr = address.ConvertToSleDeviceAddress();
        impl_.callbacks_.ForEach([connectId, &addr, errCode](std::shared_ptr<SleConnectManagerCallback> callback) {
            callback->OnPairComplete(connectId, addr, SleErrorCode(errCode));
        });
    }

    void OnReadRssiComplete(uint16_t connectId, int8_t rssi, uint32_t errCode) override
    {
        impl_.rssi_ = rssi;
        impl_.errCode_ = (SleErrorCode)errCode;
        impl_.ready_ = true;
        impl_.conditionVariable_.notify_one();
    }

private:
    SleConnectManager::SleConnectManagerImpl &impl_;
    SLE_DISALLOW_COPY_AND_ASSIGN(SleConnectCallbackStubImpl);
};
} // SleStandard
} // NearLink
} // OHOS

#endif //SLE_CONNECT_IMPL_H